﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Reflection;
using System.Data;
using System.Collections.Generic;
using System.Collections;

namespace HuoTu.Common
{
    /// <summary>
    /// 基础操作类
    /// </summary>
    public class SysBase
    {
        #region 基础函数
        /// <summary>
        /// 对象是否非空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool NotNull(object Object) { return !IsNull(Object, false); }
        /// <summary>
        /// 对象是否非空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="IsRemoveSpace">是否去除空格</param>
        /// <returns>bool值</returns>
        public static bool NotNull(object Object, bool IsRemoveSpace) { return !IsNull(Object, IsRemoveSpace); }
        /// <summary>
        /// 对象是否为空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool IsNull(object Object) { return IsNull(Object, false); }
        /// <summary>
        /// 对象是否为空
        /// 为空返回 false
        /// 不为空返回 true
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="IsRemoveSpace">是否去除空格</param>
        /// <returns>bool值</returns>
        public static bool IsNull(object Object, bool IsRemoveSpace)
        {
            if (Object == null) return true;
            string Objects = Object.ToString();
            if (Objects == "") return true;
            if (IsRemoveSpace)
            {
                if (Objects.Replace(" ", "") == "") return true;
                if (Objects.Replace("　", "") == "") return true;
            }
            return false;
        }
        /// <summary>
        /// 对象是否为bool值
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <returns>bool值</returns>
        public static bool IsBool(object Object) { return IsBool(Object, false); }
        /// <summary>
        /// 判断是否为bool值
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="Default">默认bool值</param>
        /// <returns>bool值</returns>
        public static bool IsBool(object Object, bool Default)
        {
            if (IsNull(Object)) return Default;
            try { return bool.Parse(Object.ToString()); }
            catch { return Default; }
        }

        /// <summary>
        /// 是否URL地址
        /// </summary>
        /// <param name="HttpUrl">等待验证的Url地址</param>
        /// <returns>bool</returns>
        public static bool IsHttp(string HttpUrl) { return Regex.IsMatch(HttpUrl, @"^(http|https):\/\/[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]{2,}[A-Za-z0-9\.\/=\?%\-&_~`@[\]:+!;]*$"); }
        /// <summary>
        /// 取字符左函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Left(object Object, int MaxLength)
        {
            if (IsNull(Object)) return "";
            return Object.ToString().Substring(0, Math.Min(Object.ToString().Length, MaxLength));
        }
        /// <summary>
        /// 取字符中间函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="StarIndex">开始的位置索引</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Mid(string Object, int StarIndex, int MaxLength)
        {
            if (IsNull(Object)) return "";
            if (StarIndex >= Object.Length) return "";
            return Object.Substring(StarIndex, MaxLength);
        }
        /// <summary>
        /// 取字符右函数
        /// </summary>
        /// <param name="Object">要操作的 string  数据</param>
        /// <param name="MaxLength">最大长度</param>
        /// <returns>string</returns>
        public static string Right(object Object, int MaxLength)
        {
            if (IsNull(Object)) return "";
            int i = Object.ToString().Length;
            if (i < MaxLength) { MaxLength = i; i = 0; } else { i = i - MaxLength; }
            return Object.ToString().Substring(i, MaxLength);
        }



        #endregion

        #region 字符转换函数
        /// <summary>
        /// 字符 int  转换为 char
        /// </summary>
        /// <param name="Int">字符[int]</param>
        /// <returns>char</returns>
        public static char IntToChar(int Int) { return (char)Int; }
        /// <summary>
        /// 字符 int  转换为字符 string
        /// </summary>
        /// <param name="Int">字符 int</param>
        /// <returns>字符 string</returns>
        public static string IntToString(int Int) { return IntToChar(Int).ToString(); }
        /// <summary>
        /// 字符 string  转换为字符 int
        /// </summary>
        /// <param name="Strings">字符 string</param>
        /// <returns>字符 int</returns>
        public static int StringToInt(string Strings)
        {
            if (IsNull(Strings)) return -100; char[] chars = Strings.ToCharArray(); return (int)chars[0];
        }
        /// <summary>
        /// 字符 string  转换为 char
        /// </summary>
        /// <param name="Strings">字符 string</param>
        /// <returns>char</returns>
        public static char StringToChar(string Strings) { return IntToChar(StringToInt(Strings)); }

        /// <summary>
        /// 转换为字符串
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static string ToString(object Object)
        {
            try { return Object.ToString(); }
            catch { return string.Empty; }
        }
        #endregion

        #region 操作 int  数据
        /// <summary>
        /// 对象是否为int值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static bool IsInt(object Object)
        {
            try { int.Parse(Object.ToString()); return true; }
            catch { return false; }
        }
        /// <summary>
        /// 对象是否为 int  类型数据
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="isTrue">返回是否转换成功</param>
        /// <returns>int值</returns>
        private static int IsInt(object Object, out bool isTrue)
        {
            try { isTrue = true; return int.Parse(Object.ToString()); }
            catch { isTrue = false; return 0; }
        }
        /// <summary>
        /// 转换成为 int  数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <returns>int 数据</returns>
        public static int ToInt(object Object) { return ToInt(Object, 0); }
        /// <summary>
        /// 转换成为 int  数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <returns>int 数据</returns>
        public static int ToInt(object Object, int Default) { return ToInt(Object, Default, -2147483648, 2147483647); }
        /// <summary>
        /// 转换成为 int  数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinInt"> 下界限定的最小值 , 若超过范围 , 则返回 默认值</param>
        /// <returns>int 数据</returns>
        public static int ToInt(object Object, int Default, int MinInt) { return ToInt(Object, Default, MinInt, 2147483647); }
        /// <summary>
        /// 转换成为 int  数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinInt"> 下界限定的最小值 , 若超过范围 , 则返回 默认值</param>
        /// <param name="MaxInt">上界限定的最大值 , 若超过范围 , 则返回 默认值</param>
        /// <returns>int 数据</returns>
        public static int ToInt(object Object, int Default, int MinInt, int MaxInt)
        {
            bool isTrue = false;
            int Int = IsInt(Object, out isTrue);
            if (!isTrue) return Default;
            if (Int < MinInt || Int > MaxInt) return Default;
            return Int;
        }
        #endregion

        #region 操作 long  数据

        /// <summary>
        /// 对象是否为long值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static bool IsLong(object Object)
        {
            try { long.Parse(Object.ToString()); return true; }
            catch { return false; }
        }
        /// <summary>
        /// 对象是否为 long  类型数据
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="isTrue">返回是否转换成功</param>
        /// <returns>long值</returns>
        private static long IsLong(object Object, out bool isTrue)
        {
            try { isTrue = true; return long.Parse(Object.ToString()); }
            catch { isTrue = false; return 0; }
        }
        /// <summary>
        /// 转换成为 Long 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <returns>Long 数据</returns>
        public static long ToLong(object Object) { return ToLong(Object, 0); }
        /// <summary>
        /// 转换成为 Long 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <returns>Long 数据</returns>
        public static long ToLong(object Object, long Default) { return ToLong(Object, Default, -9223372036854775808, 9223372036854775807); }
        /// <summary>
        /// 转换成为 long 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">转换不成功返回的默认值</param>
        /// <param name="MinLong">下界限定的最小值 , 若超过范围 , 则返回 默认值</param>
        /// <returns>long 数据</returns>
        public static long ToLong(object Object, long Default, long MinLong) { return ToLong(Object, Default, MinLong, 9223372036854775807); }
        /// <summary>
        /// 转换成为 long 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinLong">下界限定的最小值 , 若超过范围 , 则返回 默认值</param>
        /// <param name="MaxLong">上界限定的最大值 , 若超过范围 , 则返回 默认值</param>
        /// <returns>long 数据</returns>
        public static long ToLong(object Object, long Default, long MinLong, long MaxLong)
        {
            bool isTrue = false;
            long Long = IsLong(Object, out isTrue);
            if (!isTrue) return Default;
            if (Long < MinLong || Long > MaxLong) return Default;
            return Long;
        }
        #endregion

        #region 操作 float  数据

        /// <summary>
        /// 是否为float值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static bool IsFloat(object Object)
        {
            try { float.Parse(Object.ToString()); return true; }
            catch { return false; }
        }
        /// <summary>
        /// 对象是否为 float  类型数据
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="isTrue">返回是否转换成功</param>
        /// <returns>float值</returns>
        private static float IsFloat(object Object, out bool isTrue)
        {
            try { isTrue = true; return float.Parse(Object.ToString()); }
            catch { isTrue = false; return 0; }
        }
        /// <summary>
        /// 转换成为 float 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <returns>float 数据</returns>
        public static float ToFloat(object Object) { return ToFloat(Object, 0); }
        /// <summary>
        /// 转换成为 float 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <returns>float 数据</returns>
        public static float ToFloat(object Object, float Default) { return ToFloat(Object, Default, Convert.ToSingle(-3.4 * Math.Pow(10, 38)), Convert.ToSingle(3.4f * Math.Pow(10, 38))); }
        /// <summary>
        /// 转换成为 float 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinFloat"> 小于等于 转换成功后,下界限定的最小值,若超过范围 则返回 默认值</param>
        /// <returns>float 数据</returns>
        public static float ToFloat(object Object, float Default, float MinFloat) { return ToFloat(Object, Default, MinFloat, Convert.ToSingle(3.4f * Math.Pow(10, 38))); }
        /// <summary>
        /// 转换成为 float 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinFloat"> 下界限定的最小值 , 若超过范围 , 则返回 默认值</param>
        /// <param name="MaxFloat"> 上界限定的最大值 , 若超过范围 , 则返回 默认值</param>
        /// <returns>float 数据</returns>
        public static float ToFloat(object Object, float Default, float MinFloat, float MaxFloat)
        {
            bool isTrue = false;
            float Float = IsFloat(Object, out isTrue);
            if (!isTrue) return Default;
            if (Float < MinFloat || Float > MaxFloat) return Default;
            return Float;
        }

        /// <summary>
        /// 转换为BOOL
        /// </summary>
        /// <param name="Object"></param>
        /// <param name="Default"></param>
        /// <returns></returns>
        public static bool ToBoolen(Object Object, bool Default)
        {
            bool isTrue = false;
            try
            {
                isTrue = bool.Parse(Object.ToString());
            }
            catch
            {
                isTrue = Default;
            }
            return isTrue;
        }

        public static bool ToBoolen(Object Object)
        {
            return ToBoolen(Object, false);
        }

        #endregion

        #region 操作 decimal  数据

        /// <summary>
        /// 是否为decimal值
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        public static bool IsDecimal(object Object)
        {
            try { decimal.Parse(Object.ToString()); return true; }
            catch { return false; }
        }
        /// <summary>
        /// 对象是否为 decimal  类型数据
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="isTrue">返回是否转换成功</param>
        /// <returns>decimal值</returns>
        private static decimal IsDecimal(object Object, out bool isTrue)
        {
            try { isTrue = true; return decimal.Parse(Object.ToString()); }
            catch { isTrue = false; return 0; }
        }
        /// <summary>
        /// 转换成为 decimal 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <returns>decimal 数据</returns>
        public static decimal ToDecimal(object Object) { return ToDecimal(Object, 0); }
        /// <summary>
        /// 转换成为 decimal 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <returns>decimal 数据</returns>
        public static decimal ToDecimal(object Object, decimal Default) { return ToDecimal(Object, Default, Convert.ToDecimal(-7.9 * Math.Pow(10, 28)), Convert.ToDecimal(7.9 * Math.Pow(10, 28))); }

        /// <summary>
        /// 转换成为 decimal 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinFloat"> 小于等于 转换成功后,下界限定的最小值,若超过范围 则返回 默认值</param>
        /// <returns>decimal 数据</returns>
        public static decimal ToDecimal(object Object, decimal Default, decimal MinFloat) { return ToDecimal(Object, Default, MinFloat, Convert.ToDecimal(7.9 * Math.Pow(10, 28))); }
        /// <summary>
        /// 转换成为 decimal 数据
        /// </summary>
        /// <param name="Object">要转换的对象</param>
        /// <param name="Default">默认值</param>
        /// <param name="MinDecimal"> 下界限定的最小值 , 若超过范围 , 则返回 默认值</param>
        /// <param name="MaxDecimal"> 上界限定的最大值 , 若超过范围 , 则返回 默认值</param>
        /// <returns>decimal 数据</returns>
        public static decimal ToDecimal(object Object, decimal Default, decimal MinDecimal, decimal MaxDecimal)
        {
            bool isTrue = false;
            decimal Decimal = IsDecimal(Object, out isTrue);
            if (!isTrue) return Default;
            if (Decimal < MinDecimal || Decimal > MaxDecimal) return Default;
            return Decimal;
        }
        #endregion

        #region 操作 DateTime 数据

        /// <summary>
        /// 是否为时间格式
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="isTrue">返回是否转换成功</param>
        /// <returns>DateTime</returns>
        public static bool IsTime(object Object)
        {
            bool val = false;
            try
            {
                if (IsNull(Object)) return false;
                DateTime.Parse(Object.ToString());
                val = true;
            }
            catch
            {
                return false;
            }
            return val;
        }

        /// <summary>
        /// 是否为时间格式
        /// </summary>
        /// <param name="Object">要判断的对象</param>
        /// <param name="isTrue">返回是否转换成功</param>
        /// <returns>DateTime</returns>
        public static DateTime IsTime(object Object, out bool isTrue)
        {
            isTrue = false;
            if (IsNull(Object)) return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
            try { isTrue = true; return DateTime.Parse(Object.ToString()); }
            catch { isTrue = false; return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue; }
        }
        /// <summary>
        /// 操作 DateTime  数据
        /// </summary>
        /// <param name="Object">要操作的字符</param>
        /// <returns>DateTime</returns>
        public static DateTime ToTime(string Object) { return ToTime(Object, DateTime.Now); }
        /// <summary>
        /// 字符串转换为时间函数
        /// </summary>
        /// <param name="Object">要操作的字符</param>
        /// <param name="Default">默认时间</param>
        /// <returns>DateTime</returns>
        public static DateTime ToTime(string Object, DateTime Default)
        {
            if (IsNull(Object)) return Default;
            bool isTrue = false;
            DateTime Time = IsTime(Object, out isTrue);
            if (!isTrue) return Default;
            return Time;
        }
        /// <summary>
        /// 获得当前时间
        /// </summary>
        /// <param name="format">时间样式</param>
        /// <returns>string</returns>
        public static string ToNow(string format) { return DateTime.Now.ToString(format); }
        /// <summary>
        /// 转换字符串为格式化时间字符串
        /// </summary>
        /// <param name="Object">要操作的字符</param>
        /// <returns>string</returns>
        public static string ToTimes(string Object) { return ToTimes(Object, "yyyy-MM-dd HH:mm:ss"); }
        /// <summary>
        /// 转换字符串为格式化时间字符串
        /// </summary>
        /// <param name="Object">要操作的字符</param>
        /// <param name="format">格式化样式</param>
        /// <returns>string</returns>
        public static string ToTimes(string Object, string format) { return ToTimes(Object, DateTime.Now, format); }
        /// <summary>
        /// 转换字符串为格式化时间字符串
        /// </summary>
        /// <param name="Object">要操作的字符</param>
        /// <param name="Default">默认时间</param>
        /// <returns>string</returns>
        public static string ToTimes(string Object, DateTime Default) { return ToTimes(Object, Default, "yyyy-MM-dd HH:mm:ss"); }
        /// <summary>
        /// 转换字符串为格式化时间字符串
        /// </summary>
        /// <param name="Object">要操作的字符</param>
        /// <param name="Default">默认时间</param>
        /// <param name="format">格式化样式</param>
        /// <returns>string</returns>
        public static string ToTimes(string Object, DateTime Default, string format)
        {
            if (IsNull(Object)) return Default.ToString(format);
            bool isTrue = false;
            DateTime Time = IsTime(Object, out isTrue);
            if (!isTrue) return Default.ToString(format);
            return Time.ToString(format);
        }
        /// <summary>
        /// 获取两个时间差值（按天算）
        /// </summary>
        /// <param name="old_time"></param>
        /// <param name="new_time"></param>
        /// <returns></returns>
        public static int DateDiffDays(DateTime old_time, DateTime new_time)
        {
            TimeSpan ts = new TimeSpan(new_time.Ticks - old_time.Ticks);
            return ts.Days;
        }
        /// <summary>
        /// 获取两个时间差值（按周算）
        /// </summary>
        /// <param name="old_time"></param>
        /// <param name="new_time"></param>
        /// <returns></returns>
        public static int DateDiffWeek(DateTime old_time, DateTime new_time)
        {
            TimeSpan ts = new TimeSpan(new_time.Ticks - old_time.Ticks);
            return (ts.Days / 7);
        }
        /// <summary>
        /// 获取当天，按当前时间获取当天的剩下时间
        /// </summary>
        /// <param name="time">当前时间</param>
        /// <returns></returns>
        public static Double DateDiffMinutes(DateTime time)
        {
            TimeSpan ts = new TimeSpan(DateTime.Today.AddHours(24).Ticks - time.Ticks);
            return ts.Minutes;
        }
        #endregion

        #region 互转函数
        /// <summary>
        /// 将集合类转换成DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        //public static DataTable ToDataTable<T>(List<T> list)
        //{
        //    DataTable result = new DataTable();
        //    if (list.Count > 0)
        //    {
        //        PropertyInfo[] propertys = list[0].GetType().GetProperties();
        //        foreach (PropertyInfo pi in propertys)
        //        {
        //            result.Columns.Add(pi.Name, pi.PropertyType);
        //        }
        //        for (int i = 0; i < list.Count; i++)
        //        {
        //            ArrayList tempList = new ArrayList();
        //            foreach (PropertyInfo pi in propertys)
        //            {
        //                object obj = pi.GetValue(list[i], null);
        //                tempList.Add(obj);
        //            }
        //            object[] array = tempList.ToArray();
        //            result.LoadDataRow(array, true);
        //        }
        //    }
        //    return result;
        //}

        #endregion

    }
}
